﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SecuritiesAnalysis.ToolkitWinForm
{
    public partial class frmFavoriteManagement : Form
    {

        protected SecuritiesAnalysisDataContext ctx;//DataContext对象
        protected bool IsFavor = false;

        public frmFavoriteManagement()
        {
            InitializeComponent();
            ctx = new SecuritiesAnalysisDataContext();
        }

        protected void GridBind()
        {
            this.grdStock.DataSource = from stock in ctx.T_StockCodes
                                 orderby stock.StockCode
                                 select stock;
        }


        private void btnSetfavor_Click(object sender, EventArgs e)
        {

            T_StockCode model = ctx.T_StockCodes.First(stock=>stock.StockCode==(int)(grdStock.SelectedRows[0].Cells[0].Value));
            model.Enabled = !IsFavor;
            ctx.SubmitChanges();
            grdStock.SelectedRows[0].Cells[1].Value = !IsFavor;
        }

        private void chkShowFavor_CheckedChanged(object sender, EventArgs e)
        {
            if (chkShowFavor.Checked)
            {
                this.grdStock.DataSource = from stock in ctx.T_StockCodes
                                           where stock.Enabled == true
                                           orderby stock.StockCode
                                           select stock;
            }
            else
            {
                GridBind();
            }
        }

        private void chkShowUnFavor_CheckedChanged(object sender, EventArgs e)
        {
            if (chkShowUnFavor.Checked)
            {
                this.grdStock.DataSource = from stock in ctx.T_StockCodes
                                           where stock.Enabled == false
                                           orderby stock.StockCode
                                           select stock;
            }
            else
            {
                GridBind();
            }
        }

        private void grdStock_SelectionChanged(object sender, EventArgs e)
        {
            if (this.grdStock.SelectedRows.Count == 0)
            {
                return;
            }
            this.btnSetfavor.Enabled = true;
            this.lblStockCode.Text = ((int)(grdStock.SelectedRows[0].Cells[0].Value)).ToString("000000");
            this.lblStockCode.ForeColor = Color.Blue;

            this.btnSetfavor.Text = (bool)(grdStock.SelectedRows[0].Cells[1].Value) ? "取消关注" : "设为关注";
            this.IsFavor = (bool)(grdStock.SelectedRows[0].Cells[1].Value);
        }

        private void frmFavoriteManagement_Load(object sender, EventArgs e)
        {
            GridBind();
        }
    }
}
